<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form :inline="true" label-width="80px">
        <el-row>
          <el-col :span="10">
            <el-form-item label="充值时间">
              <el-date-picker
                v-model="rechargeData"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="机构名称">
              <el-select filterable clearable placeholder="请选择" v-model="params.hospital_id">
                <el-option
                  v-for="item in hospitalNameList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="审核状态">
              <el-select clearable placeholder="请选择" v-model="params.check_status">
                <el-option
                  v-for="item in statusList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24" />
          <el-col :span="10">
            <el-form-item label="审核时间">
              <el-date-picker
                v-model="checkData"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="户名:">
              <el-input v-model="params.bank_user_name" placeholder="请输入户名" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-button type="primary" @click="search()">查询</el-button>
            <el-button type="primary" @click="download">导出</el-button>
            <!--            <a :href=downUrl>-->

            <!--            </a>-->
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- table -->
    <div v-loading="tableLoading" class="table">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column
          type="index"
          label="序号"
          fixed="left"
          align="center"
          :index="table_index"
          width="50"
        />
        <el-table-column prop="hospital_name" align="center" label="机构名称" width="180" />
        <el-table-column prop="bank_number" align="center" label="充值卡号" width="180" />
        <el-table-column prop="bank_name" align="center" label="充值银行" width="180" />
        <el-table-column prop="bank_user_name" align="center" label="户名" width="100" />
        <el-table-column prop="money" align="center" label="充值金额" width="180" />
        <el-table-column prop="before_change" align="center" label="原账户余额" width="180" />
        <el-table-column prop="after_change" align="center" label="充值后余额" width="180" />
        <el-table-column prop="created_at" align="center" label="充值时间" width="180" />
        <el-table-column prop="remark" align="center" label="充值说明" width="180" />
        <el-table-column prop="is_invoice_cn" align="center" label="发票" width="180">
          <!--          <template slot-scope="scope">-->
          <!--            <span v-if="scope.row.is_invoice == 0">需要</span>-->
          <!--            <span v-if="scope.row.is_invoice == 1">不需要</span>-->
          <!--          </template>-->
        </el-table-column>

        <!-- 状态 -->
        <el-table-column prop="check_status_cn" label="审核状态" align="center" width="180" />
        <el-table-column prop="check_remark" label="审核说明" align="center" width="180" />
        <el-table-column prop="check_username" label="审核人" align="center" width="180" />
        <el-table-column prop="check_time" label="审核时间" align="center" width="180" />

        <el-table-column align="center" label="操作" fixed="right">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.check_status == 0"
              type="text"
              @click="editRechange(scope.row.id)"
            >审核</el-button>
            <el-button
              type="text"
              @click="toRoutes('OrganizationRechangeDetail',scope.row.id,'detail')"
            >查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagesCon">
      <div>
        <el-pagination
          :current-page="params.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="params.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 审核 -->
    <el-dialog title="审核" :visible.sync="dialogVisible" width="468px">
      <el-form
        :inline="true"
        label-width="80px"
        label-position="right"
        :model="apply"
        :rules="rules"
        ref="form"
      >
        <el-form-item label="审核意见" prop="check_status">
          <el-radio-group v-model="apply.check_status">
            <p style="display: flex;">
              <el-radio label="1">审核通过</el-radio>
              <el-radio label="2">审核驳回</el-radio>
            </p>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核说明" prop="remark">
          <el-input v-model="apply.remark" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  rechargeCheck,
  rechargeDownload,
  rechargeList,
} from "../../api/NewRetailSystem";
import { getOrgList } from "../../api/accountMange";
import commonFn from "@/utils/common";
export default {
  data() {
    var validateReturn = (rule, value, callback) => {
      if (this.apply.check_status == 2 && value === "") {
        callback(new Error("审核驳回请填写审核说明"));
      } else {
        callback();
      }
    };
    return {
      checkData: [],
      rechargeData: [],
      params: {
        page: 1,
        pageSize: 10, //
        hospital_id: "", //	机构Id
        bank_number: "", //	银行卡
        bank_user_name: "", //	户名
        check_status: "", //	审核状态id
        recharge_start_date: "", //	充值开始时间
        recharge_end_date: "", //	充值结束时间
        check_start_date: "", //	审核开始时间
        check_end_date: "", //	审核结束时间
      },
      hospitalNameList: [], //机构下拉list
      statusList: [
        {
          id: 0,
          name: "待审核",
        },
        {
          id: 1,
          name: "审核通过",
        },
        {
          id: 2,
          name: "审核驳回",
        },
      ],
      typeList: [
        {
          id: 1,
          name: "对公账户",
        },
        {
          id: 2,
          name: "对私账户",
        },
      ],
      tableData: [],
      apply: {
        id: "", //	ID
        check_status: "", //	审核状态：0-待审核，1-审核成功，2-审核驳回	是	[int]
        remark: "", //	审核说明		[string]
        check_uid: sessionStorage.getItem("userId"), // 登录用户的id
      },
      total: 0,
      tableLoading: false,
      dialogVisible: false,
      rules: {
        check_status: [
          {
            required: true,
            message: "请选择审核意见",
            trigger: "blur",
          },
        ],
        remark: [
          {
            validator: validateReturn,
            trigger: "blur",
          },
          {
            max: 50,
            message: "长度 50 个字符内",
            trigger: "blur",
          },
        ],
      },
      downUrl:
        "http://test.admin.yuepinyuemei.com/admin/hospital/recharge/export?",
    };
  },
  //新加路由保存参数
  beforeRouteLeave(to, from, next) {
    // const answer = window.confirm("当前页面搜索条件将会被保存");
    //更新 put
    let keyname = this.$route.name;
    let value = JSON.stringify(this.params);
    sessionStorage.setItem(keyname, value);
    next();
  },
  mounted() {
    if (sessionStorage.getItem(this.$route.name)) {
      this.params = JSON.parse(sessionStorage.getItem(this.$route.name));
    }
    this.getList(this.params);
    this.getHosName();
  },
  watch: {
    // 充值时间
    rechargeData(val) {
      this.params.recharge_start_date = val ? val[0] : "";
      this.params.recharge_end_date = val ? val[1] : "";
    },
    // 审核时间
    checkData(val) {
      this.params.check_start_date = val ? val[0] : "";
      this.params.check_end_date = val ? val[1] : "";
    },
    //清空弹窗内容
    dialogVisible(val) {
      if (val === false) {
        this.apply.remark = "";
        this.apply.check_status = "";
      }
    },
  },
  methods: {
    search() {
      // this.params.page = 1
      // console.log("this.params",this.params)
      this.getList(this.params);
    },
    //获取列表
    async getList(opt) {
      this.tableLoading = true;
      let res = await rechargeList(opt);
      if (res.status == 200) {
        this.tableLoading = false;
        this.tableData = res.data.data.list;
        this.total = res.data.data.total;
      } else {
        this.$message({
          message: res.data.msg,
          type: "warning",
        });
      }
    },

    // 获取机构名称
    async getHosName() {
      let res = await getOrgList();
      if (res.data.code == 200) {
        let resData = res.data.data;
        this.hospitalNameList = resData;
        this.hospitalArr = resData;
      } else {
        this.errorStatus(res.data.msg);
      }
    },

    //序号
    table_index(index) {
      return (this.params.page - 1) * this.params.pageSize + index + 1;
    },

    //审核
    editRechange(item) {
      this.dialogVisible = true;
      this.apply.id = item;
      // console.log(this.apply);
    },

    //审核提交
    async submit(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          rechargeCheck(this.apply).then((res) => {
            if (res.status == 200) {
              this.$message({
                showClose: true,
                message: "审核成功",
                type: "success",
              });
              this.dialogVisible = false;
              this.search();
            } else {
              this.$message({
                showClose: true,
                message: res.message,
                type: "error",
              });
            }
          });
        } else {
          this.$message({
            showClose: true,
            message: "有必选项没填,否则不能提交哦~~",
            type: "warning",
          });
        }
      });
    },

    //导出
    download() {
      // this.downUrl="http://test.admin.yuepinyuemei.com/admin/hospital/recharge/export?"
      //   + "hospital_id=" +this.params.hospital_id
      //   +"&"
      //   + "bank_number=" +this.params.bank_number
      //   +"&"
      //   + "bank_user_name=" +this.params.bank_user_name
      //   +"&"
      //   + "check_status=" +this.params.check_status
      //   +"&"
      //   + "recharge_start_date=" +this.params.recharge_start_date
      //   +"&"
      //   + "recharge_end_date=" +this.params.recharge_end_date
      //   +"&"
      //   + "check_start_date=" +this.params.check_start_date
      //   +"&"
      //   + "check_end_date=" +this.params.check_end_date

      let data = { ...this.params };
      delete data.page;
      delete data.pageSize;
      rechargeDownload(data)
        .then((res) => {
          commonFn.downFile("机构充值导出", res);
        })
        .catch((err) => {
          this.$message({
            showClose: true,
            message: err.message,
            type: "error",
          });
        });
    },
    // 路由跳转
    toRoutes(name, id, type) {
      // console.log(e, id, type);
      const route = { path: name, query: { id: id, type: type } };
      this.$router.push(route);
    },
    //分页
    handleSizeChange(val) {
      this.params.page = 1;
      this.params.pageSize = val;
      this.getList();
    },

    handleCurrentChange(val) {
      this.params.page = val;
      this.getList(this.params);
    },
  },
};
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
</style>
